<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;

class MemberjiluController extends AdminController {
	public function _initialize(){
        parent::_initialize();
    }
	
	 /**
     * 会员列表
     */
    public function index(){
    	
		//批量删除处理
		$id = I('id');
		//dump($id);die;
		if(!empty($id) && is_array($id)){
			$delete['id'] = array('in',$id);
			$re = M('Member')->where($delete)->setField('status',-1);
			if(!$re){
				$this->error('删除失败');
			}
		}
		
		//查询条件处理
		$realname_post = I('realname');
		$nickname_post = I('nickname');
		$type = I('type');
		$post = array('type'=>$type,'realname'=>$realname_post);
		
		
		if(!empty($realname_post)){
			$where['realname'] = array('like',"%{$realname_post}%");
		}
		if(!empty($nickname_post)){
			$where['nickname'] = array('like',"%{$nickname_post}%");
		}
		
		
		//分页处理
		$post_param = $where;
		//$post_param['order'] = $order;
		
		$count = M("Member")->where($where)->count();
		$Page  = new \Think\Page($count,20);
		setPageParameter($Page, $post_param);
		$show = $Page->show();
		$list = M('Member')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		//$summoney=M('Member')->field('money')->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->sum('money');
		//$sumjifen=M('Member')->field('jifen')->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->sum('jifen');
		//$this->assign('summoney',$summoney);
		//$this->assign('sumjifen',$sumjifen);
		$summoney=0;
		$sumjifen=0;
		$sumcishu=0;
		$sumcishu_ta=0;
		$sumnum=0;
		$sumnumprice=0;
		$ren=0;
		foreach($list  as $k=>$v){
			$fin=M('finance')->where(array('open_id'=>$v['openid']))->select();
			$jifen1=0;
			$jifen2=0;
			foreach($fin as $fk=>$fv){
				if($fv['sz_type']==1 && $fv['type']==2){
					$jifen1+=$fv['money'];
				}
			/* if($v['sz_type']==2 && $v['type']==2){
					$jifen2+=$v['money'];
				}*/
			} 
			$list[$k]['jifen']=$jifen1;/* -$jifen2 */
	
			$ren++;
			$summoney+=$v['money'];
			$sumjifen+=$jifen1;
			$f=M('rob_log');//查询积分助力记录
			$arr['f_open_id']=$v['openid'];
		    $flist=$f->where($arr)->group('open_id')->select();//帮助的所有
		    $num=0;//被助力积分
		    $numprice=0;//被助力金额
		    $cishu=$f->where(array("open_id"=>$v['openid']))->count();//他人助力次数
		    $cishu_ta=$f->where(array("f_open_id"=>$v['openid']))->count();//助力他人次数
			foreach($flist as $kk=>$vv){
	            $re= M('Member')->where(array('openid' =>$vv['open_id']))->find();//所有人的信息
            	$num=$num+$vv['f_score'];
            	$numprice=$numprice+$vv['f_price'];
			}
			$list[$k]['num']=$num;
			$list[$k]['cishu']=$cishu;
			$list[$k]['cishu_ta']=$cishu_ta;
			$list[$k]['numprice']=$numprice;
			$sumnum+=$list[$k]['num'];
			$sumcishu+=$list[$k]['cishu'];
			$sumcishu_ta+=$list[$k]['cishu_ta'];
			$sumnumprice+=$list[$k]['numprice'];
		}
        $paixu=I('paixu');
    	//dump($_GET);
    	if(isset($_GET['gtc'])){
    		$type1='cishu_ta';
    		//dump($_GET);
    		if($_GET['gtc']=='desc'){
    			$list = $this->my_sort($list,'cishu_ta',SORT_DESC,SORT_STRING);
    			$_SESSION['gtcsx']="asc";
    		}else{
    			$list = $this->my_sort($list,'cishu_ta',SORT_ASC,SORT_STRING);
    			$_SESSION['gtcsx']="desc";
    		}
    	}
    	if(isset($_GET['sheng'])){
    		$type1='cishu';
    		if($_GET['sheng']=='desc'){
    			$list = $this->my_sort($list,'cishu',SORT_DESC,SORT_STRING);
    			$_SESSION['sheng']="asc";
    		}else{
    			$list = $this->my_sort($list,'cishu',SORT_ASC,SORT_STRING);
    			$_SESSION['sheng']="desc";
    		}
    	}
    	if(isset($_GET['shi'])){
    		$type1='jifen';
    		if($_GET['shi']=='desc'){
    			$list = $this->my_sort($list,'jifen',SORT_DESC,SORT_REGULAR );
    			$_SESSION['shi']="asc";
    		}else{
    			$list = $this->my_sort($list,'jifen',SORT_ASC,SORT_REGULAR );
    			$_SESSION['shi']="desc";
    		}
    	}
    	//dump($type);
    	if(isset($_GET['cny'])){
    		$type1='numprice';
    		if($_GET['cny']=='desc'){
    			$list = $this->my_sort($list,'numprice',SORT_DESC,SORT_STRING);
    			$_SESSION['cnysx']="asc";
    		}else{
    			$list = $this->my_sort($list,'numprice',SORT_ASC,SORT_STRING);
    			$_SESSION['cnysx']="desc";
    		}
    	}
    	if(isset($_GET['zltr'])){
    		$type1='num';
    		if($_GET['zltr']=='desc'){
    			$list = $this->my_sort($list,'num',SORT_DESC,SORT_STRING);
    			$_SESSION['zltr']="asc";
    		}else{
    			$list = $this->my_sort($list,'num',SORT_ASC,SORT_STRING);	
    			$_SESSION['zltr']="desc";
    		}
    	}
		
		//$arr1 = array_map(create_function('$n', 'return $n["num"];'), $list);
		//array_multisort($arr1,SORT_NUMERIC,$list );//多维数组的排序
		//赋值给页面上
		//dump($list);
		$this->assign('ren',$ren);
    	$this->assign('summoney',$summoney);
    	$this->assign('sumjifen',$sumjifen);
    	$this->assign('sumnum',$sumnum);
    	$this->assign('sumnumprice',$sumnumprice);
		$this->assign('sumcishu',$sumcishu);
		$this->assign('sumcishu_ta',$sumcishu_ta);
		//$this->assign('sumcishu',$sumcishu);
		$this->assign('post',$post);
		$this->assign('count',$count);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
		 
    }
    public function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
    	if(is_array($arrays)){
    		foreach ($arrays as $array){
    			if(is_array($array)){
    				$key_arrays[] = $array[$sort_key];
    			}else{
    				return false;
    			}
    		}
    	}else{
    		return false;
    	}
    	array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
    	return $arrays;
    }
    public function jifen_record(){
        $openid=I('userid');
		$f=M('rob_log');//查询积分助力记录
		$flist=$f->where("f_open_id='{$openid}'")->group('open_id')->select();//帮助的所有人
		//dump($re);
		foreach($flist as $k=>$v){
            $re= M('Member')->where(array('openid' =>$v['open_id']))->find();//所有人的信息
            $flist[$k]['userid']=$re['id'];
            $flist[$k]['nickname']=$re['nickname'];
            $flist[$k]['jifen']=$re['jifen'];
            $flist[$k]['heading']=$re['headimg'];
            $flist[$k]['count']=$f->where(array("f_open_id"=>$openid,"open_id"=>$v['open_id']))->count();
            $num=$num+$v['f_score'];
		}
		$flist1=$flist; 
		//$flist1['num']=$num;
		//dump($flist1);
		$this->assign('flist1',$flist1);
		$this->display();

    }
     public function Pinqiang_record(){
        $open_id = I('userid');
        $order = M('rob_orders as o,yang_rob_goods_orders as r')->where("o.order_id = r.order_id and o.open_id='{$open_id}'")->select();
         /*  dump($order);
         foreach($order as $k=>$v){
            $time=$v['end_time']-$v['start_time'];
            if($time<=0){
                $order[$k]['time']='已结束';
            }else{
                $order[$k]['time']=date("Y-m-d H:i:s",$time);
            }
        }  */
        $this->assign('flist1',$order);
        $this->display();
    }
	
}
	
